﻿using Scommesse.Model;
using Scommesse.Persistence;
using Scommesse.View;

namespace Scommesse.Presenters
{
    public class InserimentoTesseraPresenter : IInserimentoTesseraPresenter
    {
        private readonly InserimentoTessera _form;
        private IBetCard _betCard;

        public InserimentoTesseraPresenter(InserimentoTessera form)
        {
            _form = form;
            _form.AddListener(this);
        }

        #region IInserimentoTesseraPresenter Members

        public void ClearTextBox()
        {
            _form.CodiceTessera = "";
        }

        public void SetTextBoxOnActivate()
        {
            _form.CodiceTessera = "Codice BetCard";
        }

        public void InserisciTessera()
        {
            try
            {
                _betCard = DataLoader.LoadBetCardFromFile(_form.CodiceTessera);
               if (!_betCard.Bloccata)
               {
                    new AutenticazioneTesseraPresenter(_betCard, _form);
                    _form.ErrorVisible = false;
                    _form.Visible = false;
               }
                else 
                    _form.ErrorVisible = true;
            }
            catch
            {
                _form.ErrorVisible = true;
            }
        }
        #endregion
    }
}